package com.th.supcom.pds.auditprescription.client.widget;

import java.util.List;

import net.carefx.framework.container.client.constant.FrameworkConstants;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.FormPanel.LabelAlign;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.google.gwt.event.dom.client.KeyCodes;
import com.th.supcom.pds.auditprescription.client.common.AuditPrescripInfoModuleEvents;
import com.th.supcom.pds.auditprescription.client.common.RefuseModelMockFactory;
import com.th.supcom.pds.auditprescription.client.model.RefuseReasonModel;
import com.th.supcom.pds.common.client.model.PdsSupplyModel;
import com.th.supcom.pds.sortingdrug.client.model.PdsPrescDetailInfoModel;

/**
 * @author yzhang 处方查询面板
 */
public class AuditPrescripSearchPanel extends FieldSet
{
    /** 清屏按钮. */
    private Button m_clearBtn;
    /** 拒绝窗口模板下拉框 */
    private ComboBox <RefuseReasonModel> m_comboBox;
    /** 模拟的拒绝理由模板 */
    private List <RefuseReasonModel> m_lists;
    /** 处方明细信息model */
    private PdsSupplyModel m_pdsSupplyModel;
    /** 通过按钮. */
    private Button m_passBtn;
    /** 处方号. */
    private TextField <String> m_prescription;
    /** 查询按钮. */
    private Button m_queryBtn;
    /** 拒绝按钮. */
    private Button m_refuseBtn;
    /** 拒绝窗口 */
    private Dialog m_refuseWindow;
    /** 拒绝窗口备注 */
    private TextArea m_textArea;

    public AuditPrescripSearchPanel ()
    {
        setHeading ("处方查询");
        setHeight (45);
        init ();
        initRefuseWindow ();
        addEventListeners ();
    }

    /**
     * 添加组事件
     */
    private void addEventListeners ()
    {
        m_prescription.addKeyListener (new KeyListener ()
        {
            @Override
            public void componentKeyDown (final ComponentEvent event)
            {
                if (event.getKeyCode () == KeyCodes.KEY_ENTER)
                {
                    m_pdsSupplyModel = new PdsSupplyModel ();
                    m_pdsSupplyModel.setPrescNo (m_prescription.getValue ());
                    final AppEvent event2 = new AppEvent (AuditPrescripInfoModuleEvents.QUERY_INFO);
                    event2.setData ("model", m_pdsSupplyModel);
                    Dispatcher.forwardEvent (event2);
                }

            }
        });

        m_queryBtn.addSelectionListener (new SelectionListener <ButtonEvent> ()
        {

            @Override
            public void componentSelected (final ButtonEvent ce)
            {
                m_pdsSupplyModel = new PdsSupplyModel ();
                m_pdsSupplyModel.setPrescNo (m_prescription.getValue ());
                final AppEvent event = new AppEvent (AuditPrescripInfoModuleEvents.QUERY_INFO);
                event.setData ("model", m_pdsSupplyModel);
                Dispatcher.forwardEvent (event);

            }
        });
        m_clearBtn.addSelectionListener (new SelectionListener <ButtonEvent> ()
        {
            @Override
            public void componentSelected (final ButtonEvent ce)
            {
                final AppEvent event = new AppEvent (AuditPrescripInfoModuleEvents.CLEAR_INFO);
                Dispatcher.forwardEvent (event);
            }
        });
        m_passBtn.addSelectionListener (new SelectionListener <ButtonEvent> ()
        {
            @Override
            public void componentSelected (final ButtonEvent ce)
            {
                final AppEvent event = new AppEvent (AuditPrescripInfoModuleEvents.PRESC_PASS);
                Dispatcher.forwardEvent (event);

            }
        });
        m_refuseBtn.addSelectionListener (new SelectionListener <ButtonEvent> ()
        {
            @Override
            public void componentSelected (final ButtonEvent ce)
            {
                final AppEvent event = new AppEvent (AuditPrescripInfoModuleEvents.PRESC_REFUSE);
                Dispatcher.forwardEvent (event);
            }
        });

    }

    public Button getClearBtn ()
    {
        return m_clearBtn;
    }

    public ComboBox <RefuseReasonModel> getComboBox ()
    {
        return m_comboBox;
    }

    public List <RefuseReasonModel> getLists ()
    {
        return m_lists;
    }

    public Button getPassBtn ()
    {
        return m_passBtn;
    }

    public TextField <String> getPrescription ()
    {
        return m_prescription;
    }

    public Button getQueryBtn ()
    {
        return m_queryBtn;
    }

    public Button getRefuseBtn ()
    {
        return m_refuseBtn;
    }

    public Dialog getRefuseWindow ()
    {
        return m_refuseWindow;
    }

    public TextArea getTextArea ()
    {
        return m_textArea;
    }

    private void init ()
    {
        setLayout (new ColumnLayout ());
        final FormData formData = new FormData ("100%");
        // one
        final LayoutContainer one = new LayoutContainer ();

        final FormLayout layout1 = new FormLayout ();
        layout1.setLabelAlign (LabelAlign.LEFT);
        layout1.setLabelWidth (65);
        one.setLayout (layout1);
        m_prescription = new TextField <String> ();
        m_prescription.setFieldLabel (FrameworkConstants.FORM_MUST_FILL_TIP + "处方号");
        add (one, new ColumnData (0.3));
        one.setStyleAttribute ("margin-right", "10px");
        one.add (m_prescription, formData);

        // two
        final LayoutContainer two = new LayoutContainer ();
        final RowLayout layout2 = new RowLayout ();
        two.setLayout (layout2);

        m_queryBtn = new Button ("查询(Q)");
        m_queryBtn.setWidth (60);
        two.add (m_queryBtn);
        add (two, new ColumnData (0.08));

        // three
        final LayoutContainer three = new LayoutContainer ();
        final RowLayout layout3 = new RowLayout ();
        three.setLayout (layout3);
        m_passBtn = new Button ("通过(O)");
        m_passBtn.setWidth (60);
        three.add (m_passBtn);
        add (three, new ColumnData (0.08));

        // four
        final LayoutContainer four = new LayoutContainer ();
        final RowLayout layout4 = new RowLayout ();
        four.setLayout (layout4);
        m_refuseBtn = new Button ("拒绝(N)");
        m_refuseBtn.setWidth (60);
        four.add (m_refuseBtn);
        add (four, new ColumnData (0.08));

        // five
        final LayoutContainer five = new LayoutContainer ();
        final RowLayout layout5 = new RowLayout ();
        five.setLayout (layout5);
        m_clearBtn = new Button ("清屏(R)");
        m_clearBtn.setWidth (60);
        five.add (m_clearBtn);
        add (five, new ColumnData (0.08));
        setStyleAttribute ("padding-top", "3px");
        m_prescription.focus ();
    }

    /**
     * 初始化弹出框window
     */
    private void initRefuseWindow ()
    {
        m_refuseWindow = new Dialog ();
        final FormData formData = new FormData ("90%");
        m_refuseWindow.setSize (300, 200);
        m_refuseWindow.setOnEsc (false);
        m_refuseWindow.setHeading ("拒绝理由");
        m_refuseWindow.setModal (true);
        m_refuseWindow.setButtons (Dialog.YESNO);
        m_refuseWindow.setButtonAlign (HorizontalAlignment.CENTER);

        final FormPanel panel = new FormPanel ();
        panel.setSize (300, 200);
        panel.setHeaderVisible (false);
        /** 拒绝理由 */
        ListStore <RefuseReasonModel> store;
        store = new ListStore <RefuseReasonModel> ();
        m_lists = RefuseModelMockFactory.getRefuseModel ();
        store.add (m_lists);
        m_comboBox = new ComboBox <RefuseReasonModel> ();
        m_comboBox.setEditable (false);
        m_comboBox.setFieldLabel ("模板");
        m_comboBox.setDisplayField ("name");
        m_comboBox.setTriggerAction (TriggerAction.ALL);
        m_comboBox.setValue (m_lists.get (0));
        m_comboBox.setStore (store);

        m_textArea = new TextArea ();
        m_textArea.setFieldLabel ("备注");
        m_textArea.setHeight (85);

        panel.add (m_comboBox, formData);
        panel.add (m_textArea, formData);

        m_refuseWindow.add (panel);
    }

    public void setClearBtn (final Button clearBtn)
    {
        m_clearBtn = clearBtn;
    }

    public void setPassBtn (final Button passBtn)
    {
        m_passBtn = passBtn;
    }

    public void setQueryBtn (final Button queryBtn)
    {
        m_queryBtn = queryBtn;
    }

    public void setRefuseBtn (final Button refuseBtn)
    {
        m_refuseBtn = refuseBtn;
    }

}
